import {Dropdown, Flex, type MenuProps, Space, Tag, Tooltip} from "antd";
import React from "react";
import {SecurityUserType} from "../type/SecurityUser";
import {requestDelete} from "./request";
import {ItemType} from "antd/es/menu/hooks/useItems";
import {Link} from "react-router-dom";
import GlobalHandler from "../global/GlobalHandler";

export const requestMap = new Map<string,React.ReactNode>() ;

requestMap.set("GET", <Tag color={"#178c0a"} >查询</Tag>) ;
requestMap.set("PUT", <Tag color={"#837809"} >添加</Tag>) ;
requestMap.set("POST", <Tag color={"#2162b6"} >修改</Tag>) ;
requestMap.set("DELETE", <Tag color={"#bd0fa2"} >删除</Tag>) ;
requestMap.set("ANY", <Tag color={"#4c0767"} >全部</Tag>) ;
requestMap.set("DEFAULT", <Tag color={"#1a1e2a"} >默认</Tag>) ;

/**
 * 根据id删除数据
 * @param id
 * @param url
 */
export const deleteDataById = async (id: number,url: string) => {
    const message = GlobalHandler.getInstance().getMessage() ;
    const res = await requestDelete<number>(url + "/" + id,{}).then(res => res) ;
    if (res.data > 0){
        message.success("删除数据成功") ;
    }else {
        message.warning("删除数据失败") ;
    }
    return res.data > 0 ;
} ;


/**
 * 获取请求方法标签
 * @param method 请求方法
 */
export const getMethodTag = (method: string): React.ReactNode => {
    // return methodTagMap.get(method) ;
    return requestMap.get(method) ;
};

/**
 * 获取状态标签
 * @param status 状态
 * @param trueMsg 状态为true的提示
 * @param falseMsg 状态为false的提示
 */
export const getTag =(status: boolean,trueMsg: string,falseMsg:string) => {
    if (status) {
        return <Tag color={"#167e0d"}>{trueMsg}</Tag> ;
    }else {
        return <Tag color={"#83070d"}>{falseMsg}</Tag> ;
    }
} ;

/**
 * 获取用户状态标签
 * @param _
 * @param user
 */
export const getUserStatuesTag = (_:any,user:SecurityUserType): React.ReactNode => {
    let msg = "正常" ;
    let color = "#1a8039" ;

    if (!user.enabled){
        msg = "用户禁用" ;
        color = "#cf1322" ;
    }else if (!user.accountNonLocked){
        msg = "用户锁定" ;
        color = "#460c59" ;
    }else if (!user.accountNonExpired){
        msg = "用户过期" ;
        color = "#732e11" ;
    }else if (!user.credentialsNonExpired){
        msg = "密码过期" ;
        color = "#776219" ;
    }
    return <Space>
        <Tag color={color}>{msg}</Tag>
    </Space> ;
} ;


/**
 * 处理内容太长省略提示
 */
export const ellipsisHandler = (value: any) => {
    // log.debug("处理内容太长省略提示" , value) ;
    return (
        <Tooltip placement="topLeft" title={value}>
            {value}
        </Tooltip>
    );
};


/**
 * 创建表格的操作按钮
 * @param _
 * @param record
 */
export const createAction = (_: any,record: any) => {
    // console.log("createAction",record) ;

    const items: ItemType[] = [
        {
            // icon: <DownOutlined />,
            key: '/nav/admin/user/update/' + record.id,
            label: <Link to={"./update/" + record.id } state={record}>更新数据</Link>
        },
        {
            key: '/nav/admin/user/authority',
            label: <Link to={"./authority"} state={record}>授权管理</Link>
        },
        {
            key: '/nav/admin/user/menu',
            label: <Link to={"./menu"} state={record}>菜单管理</Link>
        },
        {
            className: 'ant-dropdown-menu-delete',
            key: '/nav/admin/user/delete',
            label: '删除数据',
        }
    ] ;

    const onMenuClick: MenuProps['onClick'] = (e) => {
        // getNavigate("ColumnsUtil")(e.key,{state: record}) ;
    };

    // const actionClick = () => {
    //     getNavigate("ColumnsUtil")("/nav/admin/user/details",{state: record}) ;
    // };

    return (
        <Flex align="flex-start" gap="small" vertical>
            <Dropdown.Button type={"link"} menu={{ items, onClick: onMenuClick }}>
                <Link to={"./details"} state={record}>详情</Link>
            </Dropdown.Button>
        </Flex>
    );

} ;